package com.sx.pipeline.adapter

import android.annotation.SuppressLint
import android.util.Log
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.noober.background.view.BLTextView
import com.sx.pipeline.R
import com.sx.pipeline.app.config.OperatorNeedsType
import com.sx.pipeline.repos.http.res.MessageListVo
import com.sx.pipeline.repos.http.res.ReleaseNeedsVO
import com.sx.pipeline.repos.http.res.ReleaseSupplyVo
import com.sx.pipeline.utils.AndroidUtil
import com.sx.pipeline.utils.CountDownUtil

/**
 * @Author：YY
 * @Date：2022/2/10
 * @Describe: 运营商我的需求列表适配器
 */
class OperatorNeedsListAdapter
    (
    dataList: MutableList<ReleaseNeedsVO>
) : BaseQuickAdapter<ReleaseNeedsVO, BaseViewHolder>(
    R.layout.item_operator_needs_list,dataList
) {
    private var countDownUtil: CountDownUtil? = null

    /**
     * 取消需求
     */
    var cancelCallback: ((Int) -> Unit)? = null

    /**
     * 删除需求
     */
    var deleteCallback: ((Int) -> Unit)? = null

    override fun convert(holder: BaseViewHolder, item: ReleaseNeedsVO) {
        //创建时间
        holder.setText(R.id.tv_create_time,item.createTime)
        //标题
        holder.setText(R.id.tv_title, item.needType)
        //联系人
        holder.setText(R.id.tv_contact, "联系人：${item.contact}")
        //联系方式
        holder.setText(R.id.tv_contact_phone, "联系方式：${item.contactPhone}")
        //需求类型 0:Equipment、1:Unit、2:Serve
        when(item.needDownType){
            0->{
                holder.setText(R.id.tv_device_name,"设备名称：${item.omsNeedEquipment?.name}")
                holder.setText(R.id.tv_product_num,"产品编号：${item.omsNeedEquipment?.equipmentNo}")
                holder.setText(R.id.tv_car_num,"车牌号码：${item.omsNeedEquipment?.numberPlate}")

                holder.setVisible(R.id.tv_device_name,true)
                holder.setVisible(R.id.tv_product_num,true)
                holder.setVisible(R.id.tv_car_num,true)

                holder.setGone(R.id.tv_workload,true)
                holder.setGone(R.id.tv_address,true)
            }
            1->{
                holder.setText(R.id.tv_workload,"工作量：${item.omsNeedUnit?.quantity+item.omsNeedUnit?.unit}")
                holder.setText(R.id.tv_address,"施工地点：${item.addressDetail}")

                holder.setGone(R.id.tv_device_name,true)
                holder.setGone(R.id.tv_product_num,true)
                holder.setGone(R.id.tv_car_num,true)

                holder.setVisible(R.id.tv_workload,true)
                holder.setVisible(R.id.tv_address,true)
            }
        }
        //显示 取消需求 / 删除需求
        when(item.status){
            OperatorNeedsType.PUBLISHED.code ->{
                holder.setText(R.id.tv_status,context.getString(R.string.txt_published))
                holder.setGone(R.id.tv_delete,true)
                holder.setVisible(R.id.tv_consult,true)
            }
            OperatorNeedsType.CANCELLED.code->{
                holder.setText(R.id.tv_status,context.getString(R.string.txt_cancelled))
                holder.setVisible(R.id.tv_delete,true)
                holder.setGone(R.id.tv_consult,true)
            }
        }

        //已发布状态 并且 过期时间不为空则显示倒计时
        if (item.expiration!=null && item.status==1){
            val endtime = AndroidUtil.date2TimeStamp(item.expiration, "yyyy-MM-dd HH:mm:ss")
            if (countDownUtil == null) {
                countDownUtil = CountDownUtil()
            }
            Log.d("fsdf",""+endtime)
            //倒计时
            countDownUtil!!.start((endtime!!.toLong() * 1000), object :
                CountDownUtil.OnCountDownCallBack {
                @SuppressLint("SetTextI18n")
                override fun onProcess(
                    day: String,
                    hour: String,
                    minute: String,
                    second: String
                ) {
                    holder.setText(R.id.tv_finish_time, day + "天" + hour + ":" + minute + ":" + second)
                    holder.setVisible(R.id.tv_finish_time,true)
                }

                override fun onFinish() {}
            })
        }
        holder.getView<BLTextView>(R.id.tv_delete).setOnClickListener {
            deleteCallback?.invoke(item.id)
        }

        holder.getView<BLTextView>(R.id.tv_consult).setOnClickListener {
            cancelCallback?.invoke(item.id)
        }
    }

    fun destoryTimer(){
        if (countDownUtil!=null){
            countDownUtil?.onDestroy()
        }
    }

}